{% extends 'layout/basic.html' %}
{% load static %}
{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: #ff0000;
            position: absolute;
            font-size: 13px;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="account">
        <div class="title">用户登录</div>
        <form id="loginForm" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg">{{ field.errors.0 }}</span>
                            </div>
                            <div class="col-xs-5">
                                <img id="imgcode" src="{% url 'imgcheckcode' %}" style="width: 120px;height: 35px;" title="点击刷新">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg">{{ field.errors.0 }}</span>
                    </div>
                {% endif %}
            {% endfor %}

            <div>
                <div style="float: right;">
                    <a href="{% url 'login_sms' %}">短信验证码登录?</a>
                </div>
            </div>

            <div class="row">
                <div class="col-xs-3">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="登  录"/>
                </div>
            </div>
        </form>
    </div>

{% endblock %}

{% block js %}
    <script>
        $(function () {
            BindBtnSubmitClick();
            BindImgCodeClick();
        });

        function BindBtnSubmitClick() {
            $("#btnSubmit").click(function () {
                $(".error-msg").empty();
                $.ajax({
                    url: "{% url 'login' %}",
                    type: 'POST',
                    data: $("#loginForm").serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res);
                        if (res['status']) {
                            //登录成功
                            location.href=res.data;
                        } else {
                            //登录失败
                            $.each(res['errors'], function (key, value) {
                                $("#id_" + key).next().text(value);
                            })
                        }
                    }
                })
            })
        }

        function BindImgCodeClick() {
            $("#imgcode").click(function () {
                // 定义变量oldSrc，拿到当前id的src属性的值（图片地址）
                var oldSrc = $(this).attr('src');
                // 每点击一次就将当前id所在的src的值加一个?(用来替换图片)
                // oldSrc ->  oldSrc? -> oldSrc?? -> ...
                $(this).attr('src', oldSrc + "?");
            })
        }
    </script>
{% endblock %}